package saxion.sensorframework;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.xml.ws.Endpoint;

import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;

import saxion.sensorframework.service.ClientWebService;
import saxion.sensorframework.service.SensorWebService;

/**
 * 
 * Base class for sensorframework
 * Start client/sensor webservice
 * @author Erik & Martijn
 *
 */
public class SensorFramework {
	
	 private static Logger logger = Logger.getLogger(SensorFramework.class.getName()); /**< log4j used for logging. <a href="http://logging.apache.org/log4j/1.2/">log4j website</a>  */
	 private static final String LOG_PROPERTIES_FILE = "Log4j.properties"; /**< Location of Log4j properties file */
	 private static final String CONFIG_FILE = "config.properties"; /**< Location of project properties file */
	 private String SENSORWEBSERVICE; /**< URL for the sensorwebservice. Defined in the config file \sa CONFIG_FILE*/
	 private String ClIENTWEBSERVICE; /**< Detailed description after the member. Defined in the config file \sa CONFIG_FILE */
	 
	 private Properties properties; /**< Properties file. \sa CONFIG_FILE*/

	/**
	 * Sensor Framework
	 */
	public SensorFramework ()
	{
		initialize();
 
	}
	
	/**
	 * \brief Start webservice(s).
	 * 
	 * Use mode to define which webserive to start <br> 
	 * 0 : sensor + client <br>
	 * 1 : sensor <br>
	 * 2 : client <br>
	 * default : 1 <br>
	 * 
	 * @param mode 
	 */
	public void startWebservice(int mode)
	{
		  
			switch(mode){
				case 0:
					this.startClientWebService();
					this.startSensorWebService();
				break;
				
				case 1:
					this.startSensorWebService();
				break;
				
				case 2:
					this.startClientWebService();
				break;
				
				default:
					this.startClientWebService();
					this.startSensorWebService();
				break;
			}
 
	}
	
	/**
	 * \brief initialize variables.
	 * 
	 * creates logger, load config file
	 *
	 */
	private void initialize()
	{
		initializeLogger();
		
		properties = new Properties();
		 try {
			 properties.load(new FileInputStream(CONFIG_FILE));
		} catch (FileNotFoundException e) {
			logger.error("Config file not found {"+CONFIG_FILE+"}");
			e.printStackTrace();
		} catch (IOException e) {
			logger.error("Error loading config");
			e.printStackTrace();
		}
		 
		SENSORWEBSERVICE = properties.getProperty("SENSORWEBSERVICE");
		ClIENTWEBSERVICE = properties.getProperty("ClIENTWEBSERVICE");
		
		if(SENSORWEBSERVICE == null || ClIENTWEBSERVICE == null)
		{
			logger.error("Error loading webservice url");
			System.err.println("Error loading webservice url");
			System.exit(-1);
		}
	}

	
	/**
	 * \brief initializeLogger 
	 * 
	 * initialize the log4j by reading the properties file \sa LOG_PROPERTIES_FILE
	 */
	 private void initializeLogger()
	 {
		 
	    Properties logProperties = new Properties();

	    try
	    {
	      logProperties.load(new FileInputStream(LOG_PROPERTIES_FILE));
	      PropertyConfigurator.configure(logProperties);
	      logger.info("Logging initialized.");
	    }
	    catch(IOException e)
	    {
	      throw new RuntimeException("Unable to load logging property " + LOG_PROPERTIES_FILE);
	    }
	 }
	 
	 /**
	  * \brief Start sensorWebservice
	  * 
	  *  \sa saxion.service.SensorWebservice
	  */
	 private void startSensorWebService()
	 {
		 try{
		 Endpoint.publish(SENSORWEBSERVICE, new SensorWebService(properties));
		 logger.info("Webservice running at: "+ SENSORWEBSERVICE);
		 }
			catch(Exception e)
			{
					logger.error("SensorWebservice unable to start!");
					logger.error(e.toString());
					e.printStackTrace();
			}
	 }
	 
	 /**
	  *  \brief Start clientWebservice
	  *  
	  *  \sa saxion.service.ClientWebService
	  */
	 private void startClientWebService()
	 {
		 try{
		 Endpoint.publish(ClIENTWEBSERVICE, new ClientWebService(properties));
		 logger.info("Webservice running at: "+ClIENTWEBSERVICE);
		 }
			catch(Exception e)
			{
				logger.error("ClientWebservice unable to start!");
				logger.error(e.toString());
				e.printStackTrace();
			}
	 }
	
}
